$(document).ready(function() {
    $('body').append('<div id="back-to-top">Back to top</div>');
    $('#top').hide();
    $('#slider').nivoSlider();
    $sliderNavPos = ($('#slider').width() - $('.nivo-controlNav').width())/2;
    $('.nivo-controlNav').css('left',$sliderNavPos);
    $('.sidebar .menu ul').css('zIndex', 500);
    $(".main-menu li").each(function(){
        var $sublist = $(this).find('ul:first');
        $(this).hover(function(){
            $sublist.stop().css({overflow:"hidden",height:"auto",display:"none"}).slideDown(400,function(){
                $(this).css({overflow:"visible",height:"auto"});
            });
        },function(){
            $sublist.stop().slideUp(400,function(){
                $(this).css({overflow:"hidden",display:"none"});
            });
        });
    });
    $(".sidebar .menu li").each(function(){
        var $sublist = $(this).find('ul:first');
        $(this).hover(function(){
            $sublist.stop().css({overflow:"hidden",height:"auto",display:"none"}).slideDown(400,function(){
                $(this).css({overflow:"visible",height:"auto"});
            });
        },function(){
            $sublist.stop().slideUp(400,function(){
                $(this).css({overflow:"hidden",display:"none"});
            });
        });
    });
    $('#slider').css('zIndex', 100);
    $(window).scroll(function() {
        if ($(window).scrollTop() != 0 && $(window).width() > 800)
            $('#back-to-top').fadeIn();
        else
            $('#back-to-top').fadeOut();     
    });
    $('#back-to-top').click(function(){
       $('html, body').animate({scrollTop: 0},500);
    });
    if ($(window).width() <= 600) {
        var search = $('#search form');
        if (search)
            $('.mobile-search').append(search);
    }
    $('.leftSideArea').css('height',$('body').height());
    var menuStatus;
    var show = function(){
        if (menuStatus)
            return;
        $('body').css('overflow-x','hidden');
        $('#page').animate({
            marginLeft: '+=220px',
        }, {duration: 300, queue: false});

        $('.leftSideArea').animate({
            left: '+=220px',
        }, {duration: 300, queue: false});
        menuStatus = true
    };
    var hide = function(){
        if (!menuStatus)
            return;
        $('body').css('overflow-x','');
        $('#page').animate({
            marginLeft: '-=220px',
        }, {duration: 300, queue: false});

        $('.leftSideArea').animate({
            left: '-=220px',
        }, {duration: 300, queue: false});
        menuStatus = false
    };
    var arrow = '<span class="arrow">&nbsp;</span>';
    var back = '<li class="back">Trở lại</li>';
    var leftSideArea = $('.left-wrapper');
    var weblinks = $('ul.weblinks');
    leftSideArea.append('<h3 class="seperator">Liên kết</h3>');
    weblinks.clone().appendTo(leftSideArea);
    $('.leftSideArea li.parent').append(arrow);
    $('.leftSideArea li.parent').each(function(){
        var title = $(this).find('>a').text();
        $(this).find('>ul').prepend('<h3 class="seperator">'+title+'</h3>'+back);
    });
    
    $('span.arrow').click(function(){
        $(this).parent().find('>ul').css('display','block');
        $('.leftSideArea .left-wrapper').animate({
            left: '+=220px'
        },{ duration: 300});
    });
    $('li.back').click(function(){
        $(this).parent().css('display','none');
        $('.leftSideArea .left-wrapper').animate({
            left: '-=220px'
        },{ duration: 300});
    });
    $('.mobile-menu h1').click(function(){
        if (!menuStatus)
            show();
        else
            hide();
        return false;
    });
    $('#page').click(function(){
        if (menuStatus) {
            hide();
            return false;
        }
    });
    $('#emailForm').validate();
    $('.lists a').click(function(){
        $('.lists dd:visible').slideUp(400);
        $('.lists .active').removeClass('active');
        $(this).addClass('active').parent().next().slideDown(400);
        return false;

    });
});